﻿using SqlSugar;
using System.Data;

namespace semantic_kernel_api.DataAccess
{
    public class VectorConverter : ISugarDataConverter
    {
        public SugarParameter ParameterConverter<T>(object columnValue, int columnIndex)
        {

            // null check
            if (columnValue is not Pgvector.Vector value)
            {
                throw new ArgumentException("columnValue must be a PgVector");
            }
            var name = "@myp" + columnIndex;

            return ToSugarParameter(value, $"@vector_p_{columnIndex}");
        }

        public T QueryConverter<T>(IDataRecord dataRecord, int dataRecordIndex)
        {
            var value = dataRecord.GetValue(dataRecordIndex);
            return (T)Convert.ChangeType(value, typeof(T));
        }

        private SugarParameter ToSugarParameter(Pgvector.Vector vector, string name)
        {
            return new SugarParameter(name, null)
            {
                Value = vector,
                DbType = System.Data.DbType.Object
            };
        }
    }
}
